package com.deer.wms.system.manage.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.deer.wms.system.manage.dao.CrmDeptAreaMapper;
import com.deer.wms.system.manage.model.CrmDeptArea;
import com.deer.wms.system.manage.model.dept.Dept;
import com.deer.wms.system.manage.service.CrmDeptAreaService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
* @author 1
* @description 针对表【crm_dept_area(部门对应仓库表)】的数据库操作Service实现
* @createDate 2022-07-08 13:16:07
*/
@Service
public class CrmDeptAreaServiceImpl extends ServiceImpl<CrmDeptAreaMapper, CrmDeptArea>
implements CrmDeptAreaService {

    @Override
    public void saveArea(Dept dept) {
        //删除原表数据
        QueryWrapper<CrmDeptArea> queryWrapper = new QueryWrapper();
        queryWrapper.eq("dept_id", dept.getDeptId());
        remove(queryWrapper);

        //新增详情
        List<String> areaIds = Arrays.asList(dept.getAreaArrIds().split(","));
        //
        List<CrmDeptArea> list = new ArrayList<>();
        for (String areaId : areaIds) {
            CrmDeptArea crmDeptArea = new CrmDeptArea();
            crmDeptArea.setAreaId(Integer.valueOf(areaId));
            crmDeptArea.setDeptId(dept.getDeptId());
            list.add(crmDeptArea);
        }
        saveBatch(list);

    }

    @Override
    public List<Integer> listByDeptId(Integer deptId) {
        List<CrmDeptArea> crmDeptAreas = list(new QueryWrapper<CrmDeptArea>().in("dept_id", deptId));
        return crmDeptAreas.stream().map(CrmDeptArea::getAreaId).collect(Collectors.toList());
    }
}
